package com.hzit.servlet;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.hzit.entity.R;
import com.hzit.entity.TbBrand;
import com.hzit.entity.TbSpecification;
import com.hzit.mapper.TbBrandMapper;
import com.hzit.mapper.TbSpecificationMapper;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/spec")
public class SpecServlet extends HttpServlet {
	private TbSpecificationMapper specificationMapper;
	@Override
	public void init() throws ServletException {
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		specificationMapper = wac.getBean(TbSpecificationMapper.class);
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 处理中文乱码
		resp.setContentType("text/html;charset=utf-8");
		//2. 得到请求参数
		String cmd = req.getParameter("cmd");
		if(StrUtil.isNotBlank(cmd)){
			if("list".equals(cmd)){
				list(req,resp);
			}
		}
	}
	//2. 列表模板
	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//2.1 查询规格列表
		List<TbSpecification> specs = specificationMapper.selectByExample(null);
		//2.2 放到r对象中
		R r = R.ok().put("specs",specs);
		//2.3 输出
		resp.getWriter().println(JSON.toJSONString(r));
		resp.getWriter().flush();

	}
}
